package com.infitecs.eyas.security;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.infitecs.eyas.common.ajax.DataTablesPage;
import com.infitecs.eyas.common.ajax.DataTablesResponse;
import com.infitecs.eyas.develop.model.PermissionTreeNode;
import com.infitecs.eyas.develop.service.PermissionService;
import com.infitecs.eyas.security.model.Role;
import com.infitecs.eyas.security.service.RoleService;
import com.infitecs.eyas.security.service.UserService;
import com.infitecs.krill.ajax.response.AjaxResponse;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/security/role")
public class RoleController {
    @Autowired
    private RoleService roleService;

    @Autowired
    private UserService userService;

    @Autowired
    private PermissionService permissionService;

    @RequestMapping(method = RequestMethod.GET)
    @RequiresPermissions("security:role:open")
    public String role(Model model) throws JsonProcessingException {
        model.addAttribute("userList", userService.getUserList());
        List<PermissionTreeNode> permissionTreeNodes = permissionService.getPermissionTree();
        model.addAttribute("tree", new ObjectMapper().writeValueAsString(permissionTreeNodes));
        return "security/role";
    }

    @RequestMapping("/getRoleListPage")
    @ResponseBody
    @RequiresPermissions("security:role:open")
    public DataTablesResponse getRoleListPage(Role role,DataTablesPage dataTablesPage) {
        return new DataTablesResponse(dataTablesPage, roleService.getRoleListPage(dataTablesPage,role));
    }

    @RequestMapping(method = RequestMethod.POST)
    @ResponseBody
    @RequiresPermissions("security:role:open")
    public AjaxResponse addRole(@RequestBody Role role, HttpSession session) {
        return AjaxResponse.getInstanceByResult(roleService.addRole(role), session);
    }

    @RequestMapping(value = "/{roleId}", method = RequestMethod.DELETE)
    @ResponseBody
    @RequiresPermissions("security:role:open")
    public AjaxResponse deleteRole(@PathVariable String roleId, HttpSession session) {
        return AjaxResponse.getInstanceByResult(roleService.deleteRole(roleId), session);
    }

    @RequestMapping(value = "/{roleId}", method = RequestMethod.GET)
    @ResponseBody
    @RequiresPermissions("security:role:open")
    public AjaxResponse getRole(@PathVariable String roleId) throws JsonProcessingException {
        AjaxResponse ajaxResponse = new AjaxResponse();
        Role role = roleService.getRole(roleId);
        ajaxResponse.addAttribute("role", role);
        ajaxResponse.addAttribute("usernameList", roleService.getUserByRoleId(roleId));
        ajaxResponse.addAttribute("permTokenTree", permissionService.getPermissionTreeByRoleName(role.getRoleId()));
        return ajaxResponse;
    }

    @RequestMapping(method = RequestMethod.PUT)
    @ResponseBody
    @RequiresPermissions("security:role:open")
    public AjaxResponse updateUser(@RequestBody Role role, HttpSession session) {
        return AjaxResponse.getInstanceByResult(roleService.updateRole(role), session);
    }

}
